package com.salinity.kun.task;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.salinity.kun.algorithm.harmonic.TideSymbol;
import com.salinity.kun.helper.BaseHydrologyDataHelper;
import com.salinity.kun.helper.ConstantHelper;
import com.salinity.kun.model.StationDateData;
import com.salinity.kun.util.DateUtil;

public class TiaoHeFenXiTask implements ITask {

	@Override
	public boolean run() throws IOException, ParseException {



		int hours;
		int rawDeltaT = 5;
		// int[] intervals = { 1, 3, 6, 10, 12, 15 };
		int[] intervals = { 5 };

		Date startDate, endDate;
		List<StationDateData> lvTideList = null;
		// 72h
		List<TideSymbol> tideSymbolList = new ArrayList<>();
		tideSymbolList.add(TideSymbol.M2);
		tideSymbolList.add(TideSymbol.S2);
		tideSymbolList.add(TideSymbol.N2);
		tideSymbolList.add(TideSymbol.K2);

		tideSymbolList.add(TideSymbol.K1);
		tideSymbolList.add(TideSymbol.O1);
		tideSymbolList.add(TideSymbol.P1);
		tideSymbolList.add(TideSymbol.Q1);

		tideSymbolList.add(TideSymbol.M4);
		tideSymbolList.add(TideSymbol.MS4);
		tideSymbolList.add(TideSymbol.M6);

		SimpleDateFormat sdf = ConstantHelper.getDateFormat();

		// Path inputPath=
		// Paths.get("E:\\Kuner\\研究生\\论文相关\\data\\绿华山\\LvhuaShan_6_5min.csv");
		// Path inputPath =Paths.get("E:\\Kuner\\研究生\\论文相关\\data\\潮位数据\\LvhuaShan_7_5min.csv");
		 Path inputPath = Paths.get("E:\\Kuner\\研究生\\论文相关\\data\\潮位数据\\WeiZhouDao_7_5min.csv");
		// Path inputPath = Paths.get("E:\\Kuner\\研究生\\论文相关\\data\\潮位数据\\WeiZhouDao_7_5min.csv");
		Path outputPath = Paths.get("E:\\Kuner\\BYLW\\Jworkspace\\saltyCode\\test\\");

		startDate = sdf.parse("2018/7/15  13:00:00");
		//	startDate = sdf.parse("2018/7/1  0:00:00");
		Date lastDate = sdf.parse("2018/8/1 0:0:00");

		// hours = 742;
		  hours = 130;
		int moveMinute = 24 * 60;

		endDate = DateUtil.LDTime2Date(DateUtil.Date2LDTime(startDate).plusHours(hours + 1));
		int index = 1;
		while (endDate.before(lastDate) && index > 0) {
			lvTideList = BaseHydrologyDataHelper.getTidesFromFile(inputPath, startDate, rawDeltaT, hours, 1, 1, 2);

			endDate = DateUtil.LDTime2Date(DateUtil.Date2LDTime(startDate).plusHours(hours + 1));
			
			
			cal(startDate, endDate, rawDeltaT, intervals, hours, tideSymbolList, lvTideList,
					outputPath.resolve("WeiZhouDao" + hours));
			startDate = DateUtil.LDTime2Date(DateUtil.Date2LDTime(startDate).plusMinutes(moveMinute));
			//hours+=5;
//			if(hours>140) {
//				break;
//			}
			index--;
		}
		// startDate=DateUtil.LDTime2Date(DateUtil.Date2LDTime(startDate).plusHours(hours+1));

		//
		// //96
		// hours = 96;
		// lvTideList = BaseHydrologyDataHelper.getTidesFromFile(inputPath, rawDeltaT,
		// hours);
		//
		// startDate = sdf.parse(lvTideList.get(0).getDate());
		// endDate =
		// DateUtil.LDTime2Date(DateUtil.Date2LDTime(startDate).plusHours(hours+1));
		// cal(startDate, endDate, rawDeltaT, intervals, hours, tideSymbolList,
		// lvTideList,
		// outputPath.resolve("LvHuaShan_" + hours));
		//
		// //120
		// hours = 120;
		// lvTideList = BaseHydrologyDataHelper.getTidesFromFile(inputPath, rawDeltaT,
		// hours);
		//
		// startDate = sdf.parse(lvTideList.get(0).getDate());
		// endDate =
		// DateUtil.LDTime2Date(DateUtil.Date2LDTime(startDate).plusHours(hours+1));
		// cal(startDate, endDate, rawDeltaT, intervals, hours, tideSymbolList,
		// lvTideList,
		// outputPath.resolve("LvHuaShan_" + hours));

		// // 48
		// tideSymbolList.remove(TideSymbol.Q1);
		// hours = 48;
		//
		// lvTideList = BaseHydrologyDataHelper.getTidesFromFile(inputPath, rawDeltaT,
		// hours);
		// endDate =
		// DateUtil.LDTime2Date(DateUtil.Date2LDTime(startDate).plusHours(hours+1));
		// cal(startDate, endDate, rawDeltaT, intervals, hours, tideSymbolList,
		// lvTideList,
		// outputPath.resolve("LvHuaShan_" + hours));
		//
		// // 36
		// tideSymbolList.remove(TideSymbol.N2);
		// tideSymbolList.remove(TideSymbol.O1);
		// hours = 36;
		//
		// lvTideList = BaseHydrologyDataHelper.getTidesFromFile(inputPath, rawDeltaT,
		// hours);
		// endDate =
		// DateUtil.LDTime2Date(DateUtil.Date2LDTime(startDate).plusHours(hours+1));
		// cal(startDate, endDate, rawDeltaT, intervals, hours, tideSymbolList,
		// lvTideList,
		// outputPath.resolve("LvHuaShan_" + hours));
		//
		// // 24
		// tideSymbolList.remove(TideSymbol.S2);
		// tideSymbolList.remove(TideSymbol.MS4);
		// hours = 24;
		//
		// lvTideList = BaseHydrologyDataHelper.getTidesFromFile(inputPath, rawDeltaT,
		// hours);
		// endDate =
		// DateUtil.LDTime2Date(DateUtil.Date2LDTime(startDate).plusHours(hours+1));
		// cal(startDate, endDate, rawDeltaT, intervals, hours, tideSymbolList,
		// lvTideList,
		// outputPath.resolve("LvHuaShan_" + hours));
		// System.out.println("complete...");
		return true;

	}

	public void cal(Date startDate, Date endDate, int rawDeltaT, int[] newDeltaTArray, int hours,
			List<TideSymbol> tideSymbolList, List<StationDateData> dataList, Path outputPath)
			throws ParseException, IOException {

		// 傅里叶分解
		// FourierSeriesInput fsi = BaseHydrologyDataHelper.hydToFsi(dataList,
		// rawDeltaT);
		//
		// String[] itemArray = ConstantHelper.getOutputItems();
		//
		// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm_");
		//
		// // 傅里叶分解检验
		// CalculationHelper.writeToFile(fsi, itemArray,
		// outputPath.resolve("fsi_" + sdf.format(startDate) + hours + "_" + rawDeltaT +
		// "_.csv"));

		HarmonicAnalysisTask HATask = new HarmonicAnalysisTask();
		for (int newDeltaT : newDeltaTArray) {

			HATask.doCalculation(startDate, endDate, rawDeltaT, newDeltaT, hours, tideSymbolList, dataList,0, outputPath);
		}

		// HATask.saveParamsInOneFile(outputPath.resolve("AllCompare_" + hours +
		// ".csv"));

	}

	public static void main(String[] args) {

		TiaoHeFenXiTask lv = new TiaoHeFenXiTask();
		try {
			lv.run();
		} catch (IOException | ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
